const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const path = require("path");
const getLessLoaderConfig = ({ resources, cssModules, isDev } = {}) => {
  const lessLoderOptions = {
    javascriptEnabled: true,
  };
  const cssLoaderOptions = {};
  if (cssModules) {
    cssLoaderOptions.modules = {
      localIdentName:
        isDev || process.env.SENTRY_ENV === "dev"
          ? "[path][name]__[local]"
          : "[name]--[hash:base64:5]",
    };
  }
  const output = [
    isDev
      ? { loader: "style-loader" }
      : {
          loader: MiniCssExtractPlugin.loader,
          options: {
            publicPath: isDev ? "" : "",
          },
        },
    { loader: "css-loader", options: cssLoaderOptions },
    {
      loader: "less-loader",
      options: {
        lessOptions: lessLoderOptions,
      },
    },
  ];
  if (resources) {
    output.push({
      loader: "style-resources-loader",
      options: {
        patterns: [path.resolve(__dirname, "../src/style/*.less")],
      },
    });
  }

  return output;
};

// module.exports = [
//   // 【3-1】不需要依赖 style-resources-loader 配置的
//   {
//     test: /\.less$/,
//     include: [/node_modules[\\/]@qtrade/],
//     // 排除 node_modules/@qtrade 下的 css modules 文件
//     exclude: [/\.module(s?)\.less$/],
//     use: getLessLoaderConfig({ isDev }),
//   },
//   // 【3-2】需要依赖 style-resources-loader 配置的
//   {
//     test: /\.less$/,
//     include: [
//       path.resolve(__dirname, "../src"),
//       path.resolve(__dirname, "../apps"),
//       path.resolve(__dirname, "../packages"),
//     ],
//     // 排除 ../src 下的 css modules 文件
//     exclude: [/\.module(s?)\.less$/],
//     use: getLessLoaderConfig({ resources: true, isDev }),
//   },
//   // 【3-3】需要依赖 style-resources-loader 配置的 css modules
//   {
//     test: /\.module(s?)\.less$/,
//     include: [
//       path.resolve(__dirname, "../src"),
//       path.resolve(__dirname, "../apps"),
//       path.resolve(__dirname, "../packages"),
//       // TODO: @qtrade 里面的不要用 css modules
//       /node_modules[\\/]@qtrade/,
//     ],
//     use: getLessLoaderConfig({ resources: true, cssModules: true, isDev }),
//   },
// ];
module.exports.getLessLoaderConfig = getLessLoaderConfig;
